﻿using BusinessEntities;
using BusinessLayer.ParticipacionVecinal;
using ParticipacionVecinalWeb.Models;
using ParticipacionVecinalWeb.Models.ParticipacionVecinal;
//using ParticipacionVecinalWeb.Services.IniciativaVecinal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace ParticipacionVecinalWeb.Controllers.ParticipacionVecinal {
    public class JuntaVecinalController : Controller {


        #region "View Index"

        public ActionResult Index() {
            ViewBag.TotalIniciativa = 4;
            return View();
            //return View("BusquedaIV");
        }

        public JsonResult ListaIniciativa(int Pagina, int Paginacion) {

            Utils util = new Utils();
            /********************************************************/

            var lst = new List<IV>();
            lst.Add(new IV { ID = 1, C = "IV201505001", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 2, C = "IV201505002", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 3, C = "IV201505003", R = "Estrada" });
            lst.Add(new IV { ID = 4, C = "IV201505004", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 5, C = "IV201505005", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 6, C = "IV201505006", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 7, C = "IV201505007", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 8, C = "IV201505008", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 9, C = "IV201505009", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 10, C = "IV201505010", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 11, C = "IV201505011", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 12, C = "IV201505012", R = "Gutierrez Ki" });


            var strList = new {
                data = lst,
                total = lst.Count
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }

        #endregion

        #region "Registrar Iniciativa"

        public ActionResult RegistrarIniciativa(String Id) {
            bool fl = false;
            int id = 0;

            if (!String.IsNullOrEmpty(Id)) {
                if (int.TryParse(Id, out id)) {
                    fl = true;
                }
            }
            var oIV = new IV();

            int idUsuario = 0; // este es el id del login se obtiene de la sesion.

            oIV = new IV {
                ID = id,
                C = "[Nuevo]",
                FE = DateTime.Now.ToString("dd/MM/yyyy"),
                HO = DateTime.Now.ToString("HH:mm"),
                R = "Rivas Negro, Felipe",
                SE = "Sector 01",
                S = "Fanny Salinas",  /// subsctor
                DE = "", //descripcion
                DET = ""
            };


            if (id > 0) {

                RequerimientoBL obj = new RequerimientoBL();
                var item = obj.Get(id);

                oIV = new IV {
                    ID = item.Id,
                    C = item.Codigo,
                    FE = item.FechaRegistro.Value.ToString("dd/MM/yyyy"),
                    HO = item.FechaRegistro.Value.ToString("HH:mm"),
                    R = "Rivas Negro, Felipe",
                    SE = "Sector 01",
                    S = "Fanny Salinas",
                    DE = item.IdIV,
                    DET = item.Observacion
                };
            }
            return View(oIV);
        }

        public JsonResult BuscarResponsableArea(int idArea) {

            //var lst = new List<M>();

            var obj = new ResponsableBL();

            var item = (from x in obj.List(idArea)
                        select new R {
                            ID = x.Id,
                            N = x.Nombre,
                            AP = x.Paterno,
                            AM = x.Materno
                        }).FirstOrDefault();

            /********************************************************/


            var vjson = Json(item, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }

        [HttpPost]
        public JsonResult SaveRequerimiento(IV oIV) {

            RequerimientoBL obj = new RequerimientoBL();

            var oReq = new PVecinal_Requerimiento();
            oReq.IdArea = oIV.A;
            oReq.IdIV = oIV.DE;
            oReq.IdJefe = oIV.IJ;
            oReq.Observacion = oIV.DET;

            var item = obj.Guardar(oReq);

            var vjson = Json(new { ID = item.Id, C = item.Codigo, F = item.FechaRegistro.Value.ToString("dd/MM/yyyy") }, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }

        [HttpPost]
        public JsonResult UpdateRequerimiento(IV oIV) {

            RequerimientoBL obj = new RequerimientoBL();

            var oReq = new PVecinal_Requerimiento();
            oReq.IdArea = oIV.A;
            oReq.IdIV = oIV.DE;
            oReq.IdJefe = oIV.IJ;
            oReq.Observacion = oIV.DET;
            oReq.Id = oIV.ID;
            var item = obj.Update(oReq);

            var vjson = Json(new { ID = item.Id, C = item.Codigo, F = item.FechaRegistro.Value.ToString("dd/MM/yyyy") }, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            //  public int DeleteAll(int IdIV) {

            return vjson;

        }



        #endregion

        #region "Iniciativa Vecinal"

        public ActionResult IniciativaVecinal(int IdIni, String C, String R) {
            var oIV = new IV { ID = -1, V = new List<Models.ParticipacionVecinal.R>() };
            if (!String.IsNullOrEmpty(C)) {
                UsuarioPV oUsu = new UsuarioPV();
                var Usuario = oUsu.GetUsuario();
                IniciativaVecinalBL oIVbl = new IniciativaVecinalBL();
                var Iniciativa = oIVbl.Get(C);

                if (Iniciativa != null && Iniciativa.Id > 0) {
                    oIV.ID = Iniciativa.Id;
                    oIV.C = Iniciativa.Codigo;
                    oIV.FE = Iniciativa.FeRegistro.Value.ToString("dd/MM/yyyy");
                    oIV.HO = Iniciativa.FeRegistro.Value.ToString("HH:mm");
                    oIV.R = Usuario.Nombre;
                    oIV.SE = Usuario.Sector;
                    oIV.S = Usuario.Subsector;
                    oIV.DE = Iniciativa.Descripcion;
                    oIV.DET = Iniciativa.DetalleIV;
                    oIV.V = new List<Models.ParticipacionVecinal.R>();
                    oIV.D = R;
                    //Busqueda de vecinos :
                    VecinoBL oVecino = new VecinoBL();
                    var lst = oVecino.List(oIV.ID);
                    if (lst != null && lst.Count > 0) {
                        oIV.V = (from x in lst select new R { ID = x.Id, N = x.NroDoc, AP = x.Nombre, AM = x.Direccion }).ToList();
                    }
                }
            }
            return View(oIV);
        }

        /*public JsonResult ListaVecinoIV(int Pagina, int Paginacion, int idIniciativa) { 
            Utils util = new Utils(); 
            var lst = new List<IVV>();
            lst.Add(new IVV { D = "45878541", N = "Jose Altamirano", A = "Urb Manzanilla 43" });
            lst.Add(new IVV { D = "65456789", N = "Carlos Alvarado", A = "Urb Manzanilla 41" });
            lst.Add(new IVV { D = "09667865", N = "Marcos Paredes", A = "Urb Manzanilla 42" });
            lst.Add(new IVV { D = "45567878", N = "Daniel Gutierrez", A = "Urb Manzanilla 45" });

            var strList = new {
                data = lst,
                total = lst.Count
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue; 
            return vjson; 
        }*/


        public JsonResult ListaDocumentoIV(int Pagina, int Paginacion, int idIniciativa) {

            Utils util = new Utils();
            /********************************************************/

            var lst = new List<DA>();
            lst.Add(new DA { C = "SCT-001", N = "Acta Reunión", F = "20/03/2015" });
            lst.Add(new DA { C = "SCT-002", N = "Acta Reunión", F = "21/03/2015" });


            var strList = new {
                data = lst,
                total = lst.Count
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }


        public JsonResult ListaBusquedaIV(int Pagina, int Paginacion, String feini, String fefin, String codest) {

            /********************************************************/
            /*
               paramMap.feini = $('#txtFeIni01').val();
                paramMap.fefin = $('#txtFeFin01').val();
                paramMap.codest = $('#cboEstado01').val();
             */

            var parametros = new Parametros();
            DateTime FeInicio = new DateTime(1900, 1, 1);
            DateTime FeFin = new DateTime(1900, 1, 1);
            int IdEstado = 0; //Solo los que estan en estado "PROCESO"

            int.TryParse(codest, out IdEstado);
            if (!DateTime.TryParse(feini, out FeInicio)) {
                FeInicio = new DateTime(1900, 1, 1);
            }
            if (!DateTime.TryParse(fefin, out FeFin)) {
                FeFin = DateTime.Now.AddDays(7);
            }


            parametros.Pagina = Pagina;
            parametros.Paginacion = Paginacion;
            parametros.IdCombo = IdEstado;
            parametros.FeInicio = FeInicio;
            parametros.FeFin = FeFin;
            parametros.Texto = "";


            IniciativaVecinalBL bl = new IniciativaVecinalBL();

            var lstIV = bl.ListaIniciativa(parametros);
            /********************************************************/
            // public List<IVConsulta> ListaIniciativa(Parametros oParametros) {
            var lst = new List<IV>();

            int total = 0;
            if (lstIV != null && lstIV.Count > 0) {

                lst = (from x in lstIV
                       select new IV {
                           A = x.Id,
                           C = x.Codigo,
                           R = x.NombreDelegado,
                           DE = x.Descripcion,
                           E = x.Estado,
                           S = x.SubSector
                       }).ToList();
                total = lstIV[0].Total;
            }
            /*
            var lst = new List<IV>();
            lst.Add(new IV { C = "IV201505001", R = "Pedro Malvez", E = "En Proceso", S = "Subsector 01-01" });
            lst.Add(new IV { C = "IV201505002", R = "Maria Gutierrez", E = "En Proceso", S = "Subsector 01-01" });
            lst.Add(new IV { C = "IV201505003", R = "Juan Bernal", E = "En Proceso", S = "Subsector 01-02" });
            lst.Add(new IV { C = "IV201505004", R = "Marco Cosco", E = "En Proceso", S = "Subsector 01-03" });
            */
            var strList = new {
                data = lst,
                total = total
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;



        }

        #endregion

        #region "Requerimiento"

        public ActionResult Requerimiento() {
            return View();
        }

        public JsonResult ListaBusquedaRequerimiento(int Pagina, int Paginacion, String nro, String fnIni, String fnFin, int subsector) {


            DateTime fIni = new DateTime();
            DateTime fFin = new DateTime();

            if (!DateTime.TryParse(fnIni, out fIni)) {
                fIni = new DateTime(1900, 1, 1);
            }
            if (!DateTime.TryParse(fnFin, out fFin)) {
                fFin = DateTime.Now.AddDays(3);
            }

            /*
             Pagina
             Paginacion
             nro
             fIni
             fFin
             subsector
             */

            /********************************************************/

            var lst = new List<IV>();
            lst.Add(new IV { ID = 1, C = "IV201505001", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 2, C = "IV201505002", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 3, C = "IV201505003", R = "Estrada" });
            lst.Add(new IV { ID = 4, C = "IV201505004", R = "Gutierrez Ki" });
            lst.Add(new IV { ID = 5, C = "IV201505005", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 6, C = "IV201505006", R = "Espinoza Guille" });
            lst.Add(new IV { ID = 7, C = "IV201505007", R = "Gutierrez Ki" });
            var strList = new {
                data = lst,
                total = lst.Count
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }


        #endregion


        public JsonResult ListarIV(int Pagina, int Paginacion, String nro, String fnIni, String fnFin, String subsctor) {

            var parametros = new Parametros();

            DateTime FeInicio = new DateTime(1900, 1, 1);
            DateTime FeFin = new DateTime(1900, 1, 1);
            int IdSubSector = 0;
            bool fl = false;


            if (!DateTime.TryParse(fnIni, out FeInicio)) {
                FeInicio = new DateTime(1900, 1, 1);
            }

            if (!DateTime.TryParse(fnFin, out FeFin)) {
                FeFin = DateTime.Now.AddDays(7);
            }

            if (!int.TryParse(subsctor, out IdSubSector)) {
                IdSubSector = 0;
            }

            parametros.Pagina = Pagina;
            parametros.Paginacion = Paginacion;
            parametros.Codigo = nro;
            parametros.FeInicio = FeInicio;
            parametros.FeFin = FeFin;
            parametros.IdCombo = IdSubSector;

            int total = 0;


            RequerimientoBL obl = new RequerimientoBL();

            var lista = obl.List(parametros);

            var lst = (from x in lista
                       select new IV {
                           ID = x.Id,
                           R = "Rivas Negro, Felipe",
                           C = x.Codigo,
                           D = x.Observacion,
                           E = "Pendiente",
                           S = "SubSector 01-01"
                       }).ToList();


            var strList = new {
                data = lst,
                total = lst.Count()
            };
            var vjson = Json(strList, JsonRequestBehavior.AllowGet);
            vjson.MaxJsonLength = int.MaxValue;

            return vjson;

        }

    }
}
